package com.filldream.fastboot.core.manager;

import com.filldream.fastboot.base.mapper.CfReportMapper;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.entity.db.CfReport;
import com.filldream.fastboot.common.okutil.RobotUtil;
import com.filldream.fastboot.common.okutil.cls.dto.ClsNewsSimpleItem;
import com.filldream.fastboot.common.okutil.stock.cf.EastCfUtil;
import com.filldream.fastboot.common.util.JsonUtil;
import com.filldream.fastboot.core.service.websocket.ClsNewsServer;
import com.filldream.fastboot.core.thread.ThreadPoolUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class CfReportManager {

    @Resource
    private CfReportMapper baseMapper;

    /**
     * 获取东财最新研报
     * 及时获取，将没有的研报记录在表
     * @param stockCode 股票代码
     * @return
     */
    public R<List<CfReport>> refreshReport(String stockCode) {
        List<CfReport> addList = new ArrayList<>();
        List<CfReport> reportList = EastCfUtil.getReport(stockCode);
        if(ObjectUtils.isNotEmpty(reportList)){
            List<String> ids = baseMapper.getInfoCodes();
            for (CfReport report : reportList) {
                //不存在则新增
                boolean hasFlag = ids.stream().anyMatch(t -> t.equals(report.getInfoCode()));
                if( !hasFlag ){
                    int insert = baseMapper.insert(report);
                    if(insert > 0){
                        addList.add(report);
                    }
                }
            }
        }
        //推送消息到ws
        ThreadPoolUtil.R.getExecutor().execute(() -> {
            List<ClsNewsSimpleItem> newsList = new LinkedList<>();
            addList.forEach(t -> newsList.add(t.toClsNewsSimpleItem()));
            new ClsNewsServer().sendNewsList(newsList);
        });
        return R.autoSuccess(addList);
    }

    /**
     * 自动发送消息给机器人
     * @return
     */
    public B autoSendOwner(List<String> senOwnerList){
        List<ClsNewsSimpleItem> zrWaitList = new ArrayList<>();
        List<ClsNewsSimpleItem> sunWaitList = new ArrayList<>();
        List<CfReport> addList = baseMapper.getNotSendOwnerList(senOwnerList);
        for (CfReport report : addList) {
            List<String> sendOwnerList = report.getSendOwnerList();
            if( !sendOwnerList.contains("zr") ){
                zrWaitList.add(report.toClsNewsSimpleItem());
                sendOwnerList.add("zr");
            }
            if( !sendOwnerList.contains("sun") ){
                sunWaitList.add(report.toClsNewsSimpleItem());
                sendOwnerList.add("sun");
            }
            report.setSendOwner(JsonUtil.toJson(sendOwnerList));
            baseMapper.updateById(report);
        }
        //发消息
        RobotUtil.autoDD( sunWaitList );
        RobotUtil.autoFs( zrWaitList );
        return B.success();
    }

}
